//
// Created by 郑开心 on 15/12/11.
// Copyright (c) 2015 郑开心. All rights reserved.
//

import Foundation
import Alamofire
import SwiftyJSON

class StatusTool: NSObject {

    class func newStatusWithSinceId(sinceId: String?,success: (status: [Status]) -> Void,failure:(error: NSError?) -> Void){
        if let account = AccountTool.readAccount(){
            var parames = [String:String]()
            parames["access_token"] = account.access_token
            if sinceId != nil {
                parames["since_id"] = sinceId
            }

            Alamofire.request(.GET, "https://api.weibo.com/2/statuses/friends_timeline.json", parameters: parames).responseJSON { response in
                if let value = response.result.value {
                    var newStatuses = [Status]()
                    let data = JSON(value)
                    let statuses  = data["statuses"].arrayValue
                    for json in statuses {
                        let status = Status(json: json)
                        newStatuses.append(status)
                    }
                    success(status: newStatuses)
                }else {
                    failure(error: response.result.error)
                }
            }
        }
    }

    class func moreStatusWithMaxId(maxId: String?,success: (status: [Status]) -> Void,failure:(error: NSError?) -> Void){
        if let account = AccountTool.readAccount(){
            var parames = [String:String]()
            parames["access_token"] = account.access_token
            if maxId != nil {
                parames["max_id"] = maxId
            }

            Alamofire.request(.GET, "https://api.weibo.com/2/statuses/friends_timeline.json", parameters: parames).responseJSON { response in
                if let value = response.result.value {
                    var newStatuses = [Status]()
                    let data = JSON(value)
                    let statuses  = data["statuses"].arrayValue
                    for json in statuses {
                        let status = Status(json: json)
                        newStatuses.append(status)
                    }
                    success(status: newStatuses)
                }else {
                    failure(error: response.result.error)
                }
            }
        }
    }
}
